﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jzy.BLL.Member;
using Jzy.Model.Member;
using Jzy.UI.Web.BaseCode;
using Jzy.UI.Web.Areas.Forum.BaseCode;

namespace Jzy.UI.Web.Areas.Forum.Controllers
{
    public class MemberController : Controller
    {
        //
        // GET: /Forum/Member/

        public ActionResult Index()
        {
            return View();
        }


        public ContentResult Exist(string field,string value)
        {
            SiteMemberBO member = new SiteMemberBO();
            bool flag = member.ExistField(field, value, null);
            if (flag)
            {
                return Content("true");
            }
            else
            {
                return Content("false");
            }
        }

        public ActionResult Register()
        {
            if(!string.IsNullOrEmpty(Request["ErrorMessage"]))
            {
                this.ViewData["initjs"] = "setMessage('" + Request["ErrorMessage"] + "',false);";
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(SiteMemberInfo info)
        {
            string validateCode = Request.Form["ValidateCode"];
            
            if (validateCode != WebSession.ValidateCode)
            {
                return RedirectToAction("Register", new { ErrorMessage = "验证码错误" });
            }

            SiteMemberBO member = new SiteMemberBO();
            if (member.ExistField("LoginName", info.LoginName, null))
            {
                return RedirectToAction("Register", new { ErrorMessage = "用户名已存在" });
                
            }

            if (member.ExistField("Email", info.Email, null))
            {
                return RedirectToAction("Register", new { ErrorMessage = "电子邮件已存在" });
                
            }
            info.Name = info.LoginName;
            info.State = (int)MemberState.正常;
           
            member.Save(info);

            return RedirectToAction("Index", "Home");
        }

    }
}
